package cn.tedu.weibo1.Controller;

import cn.tedu.weibo1.mapper.WeiboMapper;
import cn.tedu.weibo1.pojo.dto.WeiboDTO;
import cn.tedu.weibo1.pojo.entity.Weibo;
import cn.tedu.weibo1.pojo.vo.UserVO;
import cn.tedu.weibo1.pojo.vo.WeiboDetailVO;
import cn.tedu.weibo1.pojo.vo.WeiboListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
public class WeiController {
    @Autowired(required = false)
    WeiboMapper mapper;
    @RequestMapping("/weibo/insert")
    public int insert(@RequestBody WeiboDTO weiboDTO, HttpSession session){
        UserVO userVO=(UserVO) session.getAttribute("user");
        if(userVO==null){
            return 2;
        }
        Weibo weibo=new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());
        mapper.insert(weibo);
        return 1;
    }
    @RequestMapping("/weibo/selects")
    public List<WeiboListVO> selects(){
        return mapper.selects();
    }
    @RequestMapping("/weibo/selectById")
    public WeiboDetailVO selectById(int id){
        return mapper.selectById(id);
    }
}
